package validator;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import command.PurchaseForm;

@Component
public class PurchaseFormValidator implements Validator {

	@Override
	public boolean supports(Class<?> clazz) {
		return PurchaseForm.class.equals(clazz);
	}
	
	@Override
	public void validate(Object target, Errors errors) {
		PurchaseForm form = (PurchaseForm) target;
		if(form.getCount()<=0){
			errors.rejectValue("count", "negativeorzero");
		}
		if(form.getComment().length()>300){
			errors.rejectValue("comment", "tooLong");
		}
	}
}